package org.fpp.jdk21study;

import java.util.concurrent.CountDownLatch;

/**
 * @author bigbird-0101
 * @date 2024-02-20 14:26
 */
public class ScopedValueStudy {
    private static final ScopedValue<String> scopedValue=ScopedValue.newInstance();

    /**
     * 可以看出 这个并不是 不同线程之间 而是类似当前线程不同组件之间的值传递
     * @param args
     */
    public static void main(String[] args) {
        CountDownLatch count=new CountDownLatch(1);
        new Thread(()-> ScopedValue.where(scopedValue,"1234")
                .run(()-> {
                    System.out.println("线程名："+Thread.currentThread().getName()+","+scopedValue.get());
                    count.countDown();
                    ScopedValue.where(scopedValue,"123456")
                            .run(()-> {
                                System.out.println("线程名："+Thread.currentThread().getName()+","+scopedValue.get());
                                count.countDown();
                            });
                    System.out.println("线程名："+Thread.currentThread().getName()+","+scopedValue.get());
                }),"线程1")
                .start();
        new Thread(()->{
            try {
                count.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            String s = scopedValue.get();
            System.out.println(s);
        },"线程2").start();
    }
}
